package com.tpc.control.util;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.mysql.jdbc.ResultSetMetaData;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;


public class Connector {

	static Connection aConnection;
	static PreparedStatement aStatement;
	private static final String sqlSelectString = "SELECT * FROM ";
	
	
	public static List<String> getReserveHeader(String start, String end)
	{
		Connection c = Connector.initialize();
		Connector.initialize(c);
		List<String> dataList = new ArrayList<String>();
		try{
			
			
			
			if(end.length() == 0 || end == null)
			{
				aStatement = aConnection.prepareStatement(sqlSelectString + "reserve_header WHERE reserve_no  = '"+start+"'");
			}
			else
			{
				aStatement = aConnection.prepareStatement(sqlSelectString + "reserve_header WHERE reserve_no  BETWEEN '" + start + "' AND '" + end+"'");
			}
			
			ResultSet rs = aStatement.executeQuery();
			ResultSetMetaData metaData = (ResultSetMetaData) rs.getMetaData();	
			while (rs.next()) {
						dataList.add(rs.getString(1));		
		    }
		      rs.last();
		}
		catch(SQLException e)
		{
			System.out.print(e);
		}
		return dataList;
	}
	public static List<String> getReserveHeaderById(String id)
	{
		Connection c = Connector.initialize();
		Connector.initialize(c);
		List<String> dataList = new ArrayList<String>();
		try{
			aStatement = aConnection.prepareStatement(sqlSelectString + "reserve_header WHERE reserve_no = id");
			ResultSet rs = aStatement.executeQuery();
			ResultSetMetaData metaData = (ResultSetMetaData) rs.getMetaData();	
			while (rs.next()) {
						dataList.add(rs.getString(1));		
		    }
		      rs.last();
		}
		catch(SQLException e)
		{
			System.out.print(e);
		}
		return dataList;
	}


	public static void initialize(Connection c) {
		try {
			aConnection = c;
			// aStatement=aConnection.createStatement();
		} catch (Exception e) {
			// Logging.error("SQL", "", e.toString());
			System.out.println(e);
		}
	}
	
	public static Connection initialize()
	{
		String urlProp = "jdbc:mysql://javatrain:3306/production_control?characterEncoding=UTF-8";
		String classProp = "com.mysql.jdbc.Driver";
		String userNameProp = "root";
		String passwordProp = "password";
		
		Properties props = new Properties();


		try
 		{ 
			Class.forName(classProp);
	    	aConnection = DriverManager.getConnection(urlProp, userNameProp, passwordProp);
	 	} catch (ClassNotFoundException e) {
			System.out.println(e);
		} catch (SQLException e) {
			System.out.println(e); 
		}
		return aConnection;
	}

}